package com.bjpowernode.mvc.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.common.client.RedisClient;
import com.bjpowernode.common.constant.AppConstants;
import com.bjpowernode.common.constant.RedisKey;
import com.bjpowernode.db.domain.ProductInfo;
import com.bjpowernode.db.mapper.ProductInfoMapper;
import com.bjpowernode.mvc.model.dto.CollectProductDTO;
import com.bjpowernode.mvc.service.inter.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
@Service
public class ProductServiceImpl implements ProductService {

    private final ProductInfoMapper productInfoMapper;
    private final RedisClient redisClient;

    /**
     * 使用mybatis-plus的分页插件实现分页功能
     *
     * @param type
     * @param pageNo
     * @return
     */

    @Override
    public IPage<ProductInfo> queryPageByType(Integer type, Integer pageNo, Integer pageSize) {

        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_type", type)
                .orderByDesc("release_time", "rate", "id");

        IPage<ProductInfo> page = new Page<>(pageNo, pageSize);
        return productInfoMapper.selectPage(page, queryWrapper);
    }

    /**
     * 获得首页展示产品的数据
     *
     * @return
     */
    @Override
    public CollectProductDTO getListProduct() {
        CollectProductDTO collectProductDTO;
        String val = redisClient.getVal(RedisKey.APP_PRODUCT_INDEX);
        if (StrUtil.isNotEmpty(val)) {
            collectProductDTO = JSONUtil.toBean(val, CollectProductDTO.class);
        } else {
            synchronized (this) {
                val = redisClient.getVal(RedisKey.APP_PRODUCT_INDEX);
                if (StrUtil.isNotEmpty(val)) {
                    collectProductDTO = JSONUtil.toBean(val, CollectProductDTO.class);
                } else {
                    collectProductDTO = queryAppProductFromDB();
                    redisClient.setVal(RedisKey.APP_PRODUCT_INDEX, JSONUtil.toJsonStr(collectProductDTO), 60, TimeUnit.MINUTES);
                }
            }
        }
        return collectProductDTO;
    }

    private CollectProductDTO queryAppProductFromDB() {
        List<ProductInfo> xinList = queryProductByType(AppConstants.PRODUCT_TYPE_XIN, 0, 1);
        List<ProductInfo> youList = queryProductByType(AppConstants.PRODUCT_TYPE_YOU, 0, 3);
        List<ProductInfo> sanList = queryProductByType(AppConstants.PRODUCT_TYPE_SAN, 0, 3);

        return new CollectProductDTO(xinList, youList, sanList);
    }

    //根据产品类型查询产品
    private List<ProductInfo> queryProductByType(int type, int offset, int rows) {
        String limitSQL = String.format("limit %d,%d", offset, rows);

        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_type", type).orderByDesc("release_time", "rate", "id").last(limitSQL);

        return productInfoMapper.selectList(queryWrapper);
    }

    /**
     * 根据产品类型查询产品详情
     *
     * @param productId
     * @return
     */

    @Override
    public ProductInfo getProductDetailById(Integer productId) {

        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", productId);

        return productInfoMapper.selectOne(queryWrapper);
    }
}
